﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class admin_add_item : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["adminname"]) == "")
        {
            Response.Write("<script language=javascript>alert('您没有登录');window.parent.document.location.href ='admin_login.aspx'</script>");
        }
        else if (Convert.ToString(Session["adminkind"]) != "product")
        {
            Response.Write("<script language=javascript>alert('您不是产品管理员，无权访问此页！');location='admin_login.aspx'</script>");
        }
        if (!IsPostBack)
        {
            OleDbConnection Conn = new OleDbConnection();
            Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("../miumiu.mdb");
            Conn.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter("select * from productkind where kindstatus='A'", Conn);
            da.Fill(ds, "productkind");
            DataRow[] rows = ds.Tables["productkind"].Select();
            foreach (DataRow row in rows)
            {
                ListItem newitem = new ListItem();
                newitem.Text = row["productkind"].ToString();
                newitem.Value = row["productkinden"].ToString();
                productkind.Items.Add(newitem);
            }

            DataSet ds1 = new DataSet();
            OleDbDataAdapter da1 = new OleDbDataAdapter("select * from productsource where sourcestatus='A' ", Conn);
            da1.Fill(ds1, "productsource");
            DataRow[] rows1 = ds1.Tables["productsource"].Select();
            foreach (DataRow row1 in rows1)
            {
                ListItem newitem1 = new ListItem();
                newitem1.Text = row1["productsource"].ToString();
                newitem1.Value = row1["productsourceen"].ToString();
                productsource.Items.Add(newitem1);
            }
            Conn.Close();
        }
    }

    protected void ClearTextBox()
    {
        producttitle.Text = string.Empty;
        introduction.Text = string.Empty;
        producturl.Text = string.Empty;
        detail.Text = string.Empty;
        //productkind.Text = string.Empty;
        //source.Text = string.Empty;
        abroadornot.Text = string.Empty;
        recommendornot.Text = string.Empty;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ClearTextBox();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string filePath = "", fileExtName = "", mFileName, mPath, mTotalPath;
        OleDbConnection Conn = new OleDbConnection();
        Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("../miumiu.mdb");
        Conn.Open(); 
        string producttitle1 = producttitle.Text.ToString();
        string introduction1 = introduction.Text.ToString();
        string producturl1 = producturl.Text.ToString();
        string detail1 = detail.Text.ToString();
        string productkinden = productkind.SelectedItem.Value;
        string productkindcn = productkind.SelectedItem.Text;
        string productsourceen = productsource.SelectedItem.Value;
        string productsourcecn = productsource.SelectedItem.Text;
        string abroadornot1 = abroadornot.Text.ToString();
        string recommendornot1 = recommendornot.Text.ToString();
        DateTime dt = Convert.ToDateTime(DateTime.Now);
        string addtime1 = dt.ToString("yyyyMMddHHmmss");
        string productnum1 = productkinden + productsourceen + addtime1;
        string addadminname1 = Convert.ToString(Session["adminname"]);
        string detailurle = detailurl.Text.ToString();
        string detail2e = detail2.Text.ToString();
        string detailurl2e = detailurl2.Text.ToString();
        string detail3e = detail3.Text.ToString();
        string detailurl3e = detailurl3.Text.ToString();
        string detail4e = detail4.Text.ToString();
        string detailurl4e = detailurl4.Text.ToString();
        string detail5e = detail5.Text.ToString();
        string detailurl5e = detailurl5.Text.ToString();

        if ("" != fileUp.PostedFile.FileName)
        {
            filePath = fileUp.PostedFile.FileName;//上传文件在客户端的路径
            fileExtName = filePath.Substring(filePath.LastIndexOf(".") + 1);//上传文件的扩展名
            if ((fileExtName != "jpg") && (fileExtName != "JPG") && (fileExtName != "BMP") && (fileExtName != "bmp") && (fileExtName != "GIF") && (fileExtName != "gif") && (fileExtName != "png") && (fileExtName != "PNG"))
            {
                Response.Write("<script lanuage=javascript>alert('上传文件格式必须为jpg,bmp,png或者gif！')</script>");
            }
            else if ((fileUp.PostedFile.ContentLength / 1024) > 2048)
            {
                Response.Write("<script lanuage=javascript>alert('上传文件太大,最大不能超过2M！')</script>");
            }
            else
            {
                try
                {
                    string path = "../image/product/" + productkinden + "/" + productsourceen + "/" + addadminname1 + "/" + addtime1 + "/";
                    mPath = Server.MapPath(path);//上传文件在服务器端放置的绝对位置
                    mFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);//上传文件的文件名
                    string totalpath1 = path + mFileName;//上传文件在服务器端放置的相对路径
                    string totalpath = totalpath1.Substring(totalpath1.IndexOf("/") + 1);
                    //修改数据库中‘图片地址’的信息

                    string sqlinsert = "insert into product_info (producttitle,productnum,picaddr,introduction,producturl,detail,productkind,abroadornot,recommendornot,addtime,source,addadminname,detailurl,detail2,detailurl2,detail3,detailurl3,detail4,detailurl4,detail5,detailurl5,beclickedtotalnumber)values" +
                        "('" + producttitle1 + "','" + productnum1 + "','" + totalpath + "','" + introduction1 + "','" + producturl1 + "','" + detail1 + "','" + productkindcn + "','" + abroadornot1 + "','" + recommendornot1 + "','" + addtime1 + "','" + productsourcecn + "','" + addadminname1 + "','" + detailurle + "','" + detail2e + "','" + detailurl2e + "','" + detail3e + "','" + detailurl3e + "','" + detail4e + "','" + detailurl4e + "','" + detail5e + "','" + detailurl5e + "',0)";
                    OleDbCommand scd = new OleDbCommand(sqlinsert, Conn);
                    scd.ExecuteNonQuery();
                    mTotalPath = mPath + mFileName;//上传文件在服务器端的绝对路径
                    if (!Directory.Exists(mPath))
                        Directory.CreateDirectory(mPath); 
                    fileUp.PostedFile.SaveAs(mTotalPath);//保存上传文件
                    Response.Write("<script lanuage=javascript>alert('添加产品成功！');location ='add_item.aspx'</script>");
                    Conn.Close();
                }
                catch (Exception err)
                {
                    Response.Write(err.ToString());
                    Response.Write("<script lanuage=javascript>alert('添加产品失败！');location ='add_item.aspx'</script>");
                    Conn.Close();
                }
            }
        }
        else
        {
            Response.Write("<script lanuage=javascript>alert('请添加图片！');location ='add_item.aspx'</script>");
            /*
            try
            {
                string totalpath = "image/123副本.jpg";
                string sqlinsert = "insert into product_info (producttitle,productnum,picaddr,introduction,producturl,detail,productkind,abroadornot,recommendornot,addtime,source,addadminname)values" +
                        "('" + producttitle1 + "','" + productnum1 + "','" + totalpath + "','" + introduction1 + "','" + producturl1 + "','" + detail1 + "','" + productkindcn + "','" + abroadornot1 + "','" + recommendornot1 + "','" + addtime1 + "','" + productsourcecn + "','" + addadminname1 + "')";
                OleDbCommand scd = new OleDbCommand(sqlinsert, Conn);
                scd.ExecuteNonQuery();
                Response.Write("<script lanuage=javascript>alert('添加产品成功！');location ='add_item.aspx'</script>");
                Conn.Close();
            }
            catch (Exception err)
            {
                Response.Write(err.ToString());
                Response.Write("<script lanuage=javascript>alert('添加产品失败！');location ='add_item.aspx'</script>");
                Conn.Close();
            }
            */
        }

    }    

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string abroadornot1 = this.abroadornot.Text.ToString();
        if (abroadornot1 == "y" || abroadornot1 == "n")
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }

    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string recommendornot1 = this.recommendornot.Text.ToString();
        if (recommendornot1 == "y" || recommendornot1 == "n")
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
}
